package com.xwtec.prototype.v1;

/**
 * @Author: zt
 * @Date: 2021/3/7 17:26
 * 实现原型的方式，浅拷贝,对象的属性是引用类型，如果引用类型不实现cloneable接口，浅拷贝出来的引用指向的同一个引用。
 */
public class Main {

    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person(8, 100, new Location("bj", 2001));
        Person person2 = (Person)person.clone();
        System.out.println(person == person2);
        System.out.println(person.location==person2.location);

    }
}

class Person implements Cloneable{

    int age;
    int score;

    Location location;

    public Person(int age, int score, Location location) {
        this.age = age;
        this.score = score;
        this.location = location;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", score=" + score +
                ", location=" + location +
                '}';
    }
}


class Location {
    String name;

    int doorNo;

    public Location(String name, int doorNo) {
        this.name = name;
        this.doorNo = doorNo;
    }

    @Override
    public String toString() {
        return "Location{" +
                "name='" + name + '\'' +
                ", doorNo=" + doorNo +
                '}';
    }
}